//
//  ForgetPswViewController.m
//  Kjs-iOS
//
//  Created by cfq on 16/6/7.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "ForgetPswViewController.h"
#import "KJSRequest.h"
#import "UIColor+KjsColor.h"
#import "CommonUtil.h"

#define CodeTimeInterval 180
#define SCREEN_W [[UIScreen mainScreen] bounds].size.width //屏幕宽度
#define SCREEN_H [[UIScreen mainScreen] bounds].size.height //屏幕高度
#define NAV_BAR_H self.navigationController.navigationBar.frame.size.height

@interface ForgetPswViewController ()
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *resetCodeBtn;

@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pswTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPswTF;
@property (weak, nonatomic) IBOutlet UITextField *codeTF;

@property (weak, nonatomic) IBOutlet UIButton *pswEyeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmPswEyeBtn;

@property (strong,nonatomic) NSString *tempStr;

@property (nonatomic,assign) int secondsCountDown;//倒计时总时长
@property (nonatomic,strong) NSTimer *countDownTimer;
@end

@implementation ForgetPswViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.getCodeBtn.layer.cornerRadius = 15;
    self.confirmCodeBtn.layer.cornerRadius = 10;
    self.resetCodeBtn.layer.cornerRadius = 10;
    
    [self.navigationItem setTitle:@"重置密码"];
    self.pswTF.secureTextEntry = YES;
    self.confirmPswTF.secureTextEntry = YES;
    
    [self.confirmCodeBtn addTarget:self action:@selector(updateLoginPassword) forControlEvents:UIControlEventTouchUpInside];
    [self.getCodeBtn addTarget:self action:@selector(sendMessage) forControlEvents:UIControlEventTouchUpInside];
    [self.resetCodeBtn addTarget:self action:@selector(initConfiguration) forControlEvents:UIControlEventTouchUpInside];
    self.secondsCountDown = CodeTimeInterval;

    self.pswEyeBtn.tag = 1;
    [self.pswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    self.confirmPswEyeBtn.tag = 2;
    [self.confirmPswEyeBtn addTarget:self action:@selector(swichPasswordVisiable:) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self initConfiguration];

}
#pragma mark - custom method
-(void)swichPasswordVisiable:(id)sender
{
    UIButton *eyeBtn = (UIButton *)sender;
    switch (eyeBtn.tag) {
        case 1:
        {
            
            self.tempStr = self.pswTF.text;
            self.pswTF.text = @"";
            self.pswTF.secureTextEntry = !self.pswTF.secureTextEntry;
            self.pswTF.text = self.tempStr;
        }
            break;
        case 2:
        {
            self.tempStr = self.confirmPswTF.text;
            self.confirmPswTF.text = @"";
            self.confirmPswTF.secureTextEntry = !self.confirmPswTF.secureTextEntry;
            self.confirmPswTF.text = self.tempStr;
        }
            break;
        default:
            break;
    }
    
}
-(void)updateLoginPassword
{
    if ([self.phoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else if ([self.pswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入密码" message:nil waitingTime:1.5];
    }else if (![CommonUtil validatePassword:self.pswTF.text]) {
        [GUATool showDialog:@"密码必须是6-16位的数字与字母的组合" message:nil waitingTime:1.5];
    }else if ([self.confirmPswTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请再次输入密码" message:nil waitingTime:1.5];
    }else if (![self.pswTF.text isEqualToString:self.confirmPswTF.text]){
        [GUATool showDialog:@"两次输入的密码不一致" message:nil waitingTime:1.5];
    }else if ([self.codeTF.text isEqualToString:@""]){
        [GUATool showDialog:@"请输入验证码" message:nil waitingTime:1.5];
    }else{
        [KJSRequest updateLoginPswAcceptMessageWithNewPw:self.pswTF.text phoneNumber:self.phoneTF.text verificationCode:self.codeTF.text ret:^(BasicResult *model) {
            if ([model.returnMsg isEqualToString:@"success"]) {
                [GUATool showDialog:@"密码修改成功" message:nil waitingTime:1.5];
                self.updatePswSuccess(self.phoneTF.text,self.pswTF.text);
                [self dismiss:YES];
            }
        }];
    }
    
    
}
-(void)sendMessage
{
    self.confirmCodeBtn.enabled = YES;
    [self.confirmCodeBtn setBackgroundColor:[UIColor kjsMainColor]];
    if ([self.phoneTF.text isEqualToString:@""]) {
        [GUATool showDialog:@"请输入手机号" message:nil waitingTime:1.5];
    }else if (![CommonUtil validateMobile:self.phoneTF.text]) {
        [GUATool showDialog:@"请输入正确的手机号" message:nil waitingTime:1.5];
    }else{
        [KJSRequest updateLoginPswSendMessageWithPhoneNumber:self.phoneTF.text ret:^(BasicResult *model) {
            if ([model.messageresult isEqualToString:@"0"]) {
                [GUATool showDialog:@"验证码已发送" message:nil waitingTime:1.5];
                self.countDownTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            }
        }];
    }

}

-(void)initConfiguration
{
    self.phoneTF.text = @"";
    self.pswTF.text = @"";
    self.confirmPswTF.text = @"";
    self.codeTF.text = @"";
    self.confirmCodeBtn.enabled = NO;
    [self.confirmCodeBtn setBackgroundColor:[UIColor lightGrayColor]];
}
-(void)timeFireMethod{
    
    self.secondsCountDown--;
    //去除频闪
    self.getCodeBtn.titleLabel.text = [NSString stringWithFormat:@"%d秒",self.secondsCountDown];
    [self.getCodeBtn setTitle:[NSString stringWithFormat:@"%d秒",self.secondsCountDown] forState:UIControlStateNormal];
    self.getCodeBtn.enabled = NO;
    if (self.secondsCountDown == 0) {
        [self.countDownTimer invalidate];
        [self.getCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        self.getCodeBtn.enabled = YES;
        self.secondsCountDown = CodeTimeInterval;
    }
}
-(void)changePswVisible:(id)sender
{
    
    UIButton *btn = (UIButton *)sender;
    NSLog(@"here:%ld",btn.tag);
    switch (btn.tag) {
        case 1:
            self.pswTF.secureTextEntry = !self.pswTF.secureTextEntry;
            break;
        case 2:
            self.confirmPswTF.secureTextEntry = !self.confirmPswTF.secureTextEntry;
            break;
        default:
            break;
    }
}

@end
